package asteroids.model.programs.parsing.statements;

import java.util.List;
import java.util.Map;

import be.kuleuven.cs.som.annotate.Basic;

import asteroids.model.Ship;
import asteroids.model.programs.parsing.expressions.*;
import asteroids.model.programs.parsing.types.*;

/**
 * A class of while statements.
 * A while statement executes a statement as long as a condition expression returns true.
 * 
 * @author Thijs Dieltjens - Wim Konings
 * @version 2.0
 */
public class WhileStatement extends Statement {

	public WhileStatement(Expression e, Statement s,int line, int column){
		super(line,column);
		this.expression = e;
		this.statement=s;
	}

	@Basic
	public Expression getExpression() {
		return expression;
	}

	private Expression expression;

	@Basic
	public Statement getStatement() {
		return statement;
	}

	private Statement statement;

	@Override
	public void execute(Ship ship) {
		if(getExpression().eval(ship, null) instanceof BooleanLiteral){
			while(((BooleanLiteral) getExpression().eval(ship, null)).getValue()==true){
				getStatement().execute(ship);
			}
		}
	}

	@Override
	public List<String> getTypeCheckErrors(List<String> errors, Map<String, Type> globals) {
		if(getExpression().eval(null, globals).getClass()!=BooleanLiteral.class){
			errors.add(getLine()+"-"+getColumn()+": Invalid while-condition");
		}
		errors= getStatement().getTypeCheckErrors(errors, globals);
		errors = getExpression().getTypeCheckErrors(errors, globals);
		return errors;
	}

	@Override
	public boolean containsActionStatement() {
		return getStatement().containsActionStatement();
	}

}
